-- Upload.applescript
-- Upload: A simple application that uploads the running user's ~/Sites folder via rsync to a given host, using checksums for comparison. This enables diff-uploading for iWeb sites; iWeb rewrites the entire folder tree, so timestamps are useless.

--  Created by Link M Dupont on 11/8/07.
--  Copyright 2007 Link Dupont. All rights reserved.

property username : ""
property remoteHost : ""
property remotePath : ""

on awake from nib theObject
	(*Not sure if I actually need a handler for this.*)
end awake from nib

on clicked theObject
	if name of theObject is "upload" then
		if username is "" or remoteHost is "" or remotePath is "" then
			display alert "Error!" attached to window "main"
		else
			runTheUpload()
		end if
	else if name of theObject is "close" then
		tell window "preferences"
			set username to contents of text field "username"
			set remoteHost to contents of text field "host"
			set remotePath to contents of text field "path"
		end tell
		hide window "preferences"
	end if
end clicked

on runTheUpload()
	tell window "main" to tell progress indicator "progressBar" to start
	set theString to "rsync -ac --delete --exclude \"*robots.txt*\" --exclude \".ht*\" ~/Sites/ " & username & "@" & remoteHost & ":~/" & remotePath
	-- This runs the rsync in the background, silencing its output. It then prints "done" into /tmp/com.linkdupont.Upload.done.
	do shell script theString
	tell window "main" to tell progress indicator "progressBar" to stop
	display alert "Upload Finished!" attached to window "main"
end runTheUpload

on should quit after last window closed theObject
	return true
end should quit after last window closed

-- Called by File's owner early in application startup
on will finish launching theObject
	tell user defaults
		make new default entry at end of default entries with properties {name:"username", contents:""}
		make new default entry at end of default entries with properties {name:"remoteHost", contents:""}
		make new default entry at end of default entries with properties {name:"remotePath", contents:""}
		register
	end tell
	
	tell user defaults
		set username to contents of default entry "username"
		set remoteHost to contents of default entry "remoteHost"
		set remotePath to contents of default entry "remotePath"
	end tell
end will finish launching

on will quit theObject
	tell user defaults
		set contents of default entry "username" to username
		set contents of default entry "remoteHost" to remoteHost
		set contents of default entry "remotePath" to remotePath
	end tell
end will quit

on choose menu item theObject
	show window "preferences"
end choose menu item

on will close theObject
	tell window "preferences"
		set username to contents of text field "username"
		set remoteHost to contents of text field "host"
		set remotePath to contents of text field "path"
	end tell
	hide window "preferences"
end will close

on will open theObject
	if name of theObject is "preferences" then
		if username is not "" then
			tell window "preferences" to set contents of text field "username" to username
		end if
		if remoteHost is not "" then
			tell window "preferences" to set contents of text field "host" to remoteHost
		end if
		if remotePath is not "" then
			tell window "preferences" to set contents of text field "path" to remotePath
		end if
	end if
end will open

on launched theObject
	(*Add your script here.*)
end launched

